﻿using AutoMapper;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tools.Core;
using Tools.Core.Dtos;
using Tools.Core.Entities;
using Tools.Core.Extensions;
using Tools.Services.Interfaces;

namespace Tools.Modules.Navigations.ViewModels
{
    public class MassEmailViewModel : BindableBase, INavigationAware
    {
        private readonly IDialogService _ds;
        private readonly IMailReceiverService _mrs;
        private readonly IMailSenderService _mss;
        private readonly IMailMessageService _mms;
        private readonly IMapper _mapper;

        private ObservableCollection<MailReceiverDto> _receivers=new ObservableCollection<MailReceiverDto>();
        public ObservableCollection<MailReceiverDto> Receivers
        {
            get => _receivers;
            set => SetProperty(ref _receivers, value);
        }
        private ObservableCollection<MailSenderDto> _senders=new ObservableCollection<MailSenderDto>();
        public ObservableCollection<MailSenderDto> Senders
        {
            get => _senders;
            set=>SetProperty(ref _senders, value);
        }

        private ObservableCollection<MailMessageDto> _messages=new ObservableCollection<MailMessageDto>();
        public ObservableCollection<MailMessageDto> Messages
        {
            get => _messages;
            set => SetProperty(ref _messages, value);
        }

        public DelegateCommand<MailMessageDto> ShowMessageDialogCommand { get; private set; }
        public DelegateCommand<MailReceiverDto> ShowReceiverDialogCommand { get; private set; }
        public DelegateCommand<MailSenderDto> ShowSenderDialogCommand { get; private set; }

        public DelegateCommand MassSendCommand { get; private set; }
        public MassEmailViewModel(IDialogService ds,IMailReceiverService mrs,IMailSenderService mss,IMailMessageService mms,IMapper mapper)
        {
            _ds = ds;
            _mss = mss;
            _mrs = mrs;
            _mms = mms;
            _mapper = mapper;
            LoadDataAsync().Wait();
            MassSendCommand = new DelegateCommand(() =>
            {
                var sender = Senders.FirstOrDefault(s => s.IsChecked);
                var receivers = Receivers.Where(r => r.IsChecked);
                var message = Messages.FirstOrDefault(m => m.IsChecked);
                foreach (var receiver in receivers)
                {
                    Softwares.SendMails(sender, receiver, message);
                }
            });
            ShowMessageDialogCommand = new DelegateCommand<MailMessageDto>(m =>
            {
                _ds.ShowDialog("MailMessageView", new DialogParameters
                {
                    {"message",m }
                }, result => { });
            });
            ShowReceiverDialogCommand = new DelegateCommand<MailReceiverDto>(r =>
            {
                _ds.ShowDialog("MailReceiverView", new DialogParameters
                {
                    {"receiver",r }
                }, result => { });
            });
            ShowSenderDialogCommand = new DelegateCommand<MailSenderDto>(s =>
            {
                _ds.ShowDialog("MailSenderView", new DialogParameters
                {
                    {"sender",s }
                }, r => { });
            });

        }
        private async Task LoadDataAsync()
        {
            var receivers = await _mrs.GetAllReceiversAsync();
            Receivers = _mapper.Map<List<MailReceiverDto>>(receivers).AsObservableCollection();
            var senders = await _mss.GetAllSendersAsync();
            Senders=_mapper.Map<List<MailSenderDto>>(senders).AsObservableCollection();
            var messages= await _mms.GetAllMessagesAsync();
            Messages = _mapper.Map<List<MailMessageDto>>(messages).AsObservableCollection();
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
    }
}
